package cn.school.workmgr.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.school.common.utils.SysUserUtils;
import cn.school.workmgr.dao.HygieneDormitoryDao;
import cn.school.workmgr.entity.HygieneDormitory;
import cn.school.workmgr.entity.HygieneDormitoryStatistics;
import cn.school.workmgr.service.HygieneDormitoryService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @className: HygieneDormitoryServiceImpl
 * @author: fangjin.tao
 * @date: 2022/12/8
 **/
@Service
public class HygieneDormitoryServiceImpl extends ServiceImpl<HygieneDormitoryDao, HygieneDormitory> implements HygieneDormitoryService {
	@Autowired
	 private HygieneDormitoryDao hygieneDormitoryDao;

	@Override
	public IPage<HygieneDormitory> pageSql(Map<String, Object> params) {

		// 条件获取
		int pageNum = Integer.parseInt((String) params.get("pageNum"));
		int pageSize = Integer.parseInt((String) params.get("pageSize"));

		String hyshDate = (String) params.get("hyshDate"); // 卫生日期
		String hyshFlag = (String) params.get("hyshFlag"); // 卫生标记
		String createByName = (String) params.get("createByName"); // 创建人名称
		String buildName = (String) params.get("buildName"); // 楼栋名称
		String dormName = (String) params.get("dormName"); // 宿舍名称

		Page<HygieneDormitory> page = new Page<>();
		page.setSize(pageSize);
		page.setCurrent(pageNum);

		// queryWrapper查询设置
		QueryWrapper<HygieneDormitory> queryWrapper = new QueryWrapper<>();
		if (StrUtil.isNotEmpty(hyshDate)) {
			// 具体日期
			queryWrapper.ge("hydo.hydo_date", DateUtil.parse(hyshDate + " 00:00:00", "yyyy-MM-dd HH:mm:ss"));
			queryWrapper.le("hydo.hydo_date", DateUtil.parse(hyshDate + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
		}

		if (StrUtil.isNotEmpty(hyshFlag)) {
			queryWrapper.eq("hydo.hydo_flag", hyshFlag);
		}
		if (StrUtil.isNotEmpty(createByName)) {
			queryWrapper.like("hydo.create_by_name", createByName);
		}
		if (StrUtil.isNotEmpty(buildName)) {
			queryWrapper.like("build.location_name", buildName);
		}
		if (StrUtil.isNotEmpty(dormName)) {
			queryWrapper.like("hydo.dorm_name", dormName);
		}
		//租户
		queryWrapper.eq("hydo.tenant_id", SysUserUtils.getSysUser().getTenantId());
		// 排序
		queryWrapper.orderByDesc("hydo.hydo_date");

		List<HygieneDormitory> dtoList = this.baseMapper.getListBySql(page, queryWrapper);
		page.setRecords(dtoList);
		return page;
	}

	/**
	 * 生成宿舍卫生统计表
	 * @param dormId
	 * @param
	 * @return
	 */
	@Override
	public HygieneDormitoryStatistics getHygieneDormitoryDao(long dormId) {
		//根据宿舍id查询出该宿舍卫生检查信息
		QueryWrapper<HygieneDormitory> hygieneDormitoryQueryWrapper = new QueryWrapper<>();
		hygieneDormitoryQueryWrapper.eq("dorm_id",dormId);
		List<HygieneDormitory> hygieneDormitories = hygieneDormitoryDao.selectList(hygieneDormitoryQueryWrapper);

		//封装成宿舍卫生统计表
		HygieneDormitoryStatistics hygSta = new HygieneDormitoryStatistics();
		hygSta.setInspectNum((long)(hygieneDormitories.size()));//添加检查次数
		hygSta.setBuildId(hygieneDormitories.get(0).getBuidId());//添加宿舍楼栋id
		hygSta.setBuildLevelNo(hygieneDormitories.get(0).getBuildLevelNo());//添加宿舍楼层号
		hygSta.setDormId(hygieneDormitories.get(0).getDormId());//添加宿舍id
		hygSta.setDormName(hygieneDormitories.get(0).getDormName());//添加宿舍名称
		//添加宿舍评级次数
		for(HygieneDormitory hygDor:hygieneDormitories){
			if(hygDor.getHydoFlag().equals("A")){
				hygSta.setExcellentNum(hygSta.getExcellentNum()+0L);
				continue;
			}
			if(hygDor.getHydoFlag().equals("B")){
				hygSta.setGoodNum(hygSta.getGoodNum()+0L);
				continue;
			}
			if(hygDor.getHydoFlag().equals("C")){
				hygSta.setUnqualifiedNum(hygSta.getUnqualifiedNum()+0L);
				continue;
			}
		}

		return hygSta;
	}

	@Override
	public HygieneDormitory getHygieneDormitorById(long id) {
		return hygieneDormitoryDao.selectById(id);
	}

}
